package com.huhu.contact.slice;

import com.huhu.contact.ResourceTable;
import com.huhu.contact.UrgentAbility;
import com.huhu.contact.utils.Toast;
import com.huhu.contact.widget.controller.FormController;
import com.huhu.contact.widget.controller.FormControllerManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.ShapeElement;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

public class UrgentAbilitySlice extends AbilitySlice {

    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x01, UrgentAbilitySlice.class.getName());

    private TextField nameTf1;
    private TextField phoneTf1;
    private TextField nameTf2;
    private TextField phoneTf2;
    private Button confirm;
    private Button cancel;
    private UrgentAbility mUrgentAbility;
    private long mFormId;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_urgent);
        mUrgentAbility = (UrgentAbility) getAbility();
        mUrgentAbility.setUrgentAbilitySlice(this);
        initView();
        initListener();
    }

    private void initView() {
        nameTf1 = (TextField) findComponentById(ResourceTable.Id_nameTf1);
        phoneTf1 = (TextField) findComponentById(ResourceTable.Id_phoneTf1);
        nameTf2 = (TextField) findComponentById(ResourceTable.Id_nameTf2);
        phoneTf2 = (TextField) findComponentById(ResourceTable.Id_phoneTf2);
        confirm = (Button) findComponentById(ResourceTable.Id_confirm);
        cancel = (Button) findComponentById(ResourceTable.Id_cancel);
//        confirm.setEnabled(false);
    }

    private void initListener() {
        phoneTf1.addTextObserver((text, i, i1, i2) -> {
            if (!text.isEmpty() && !phoneTf2.getText().isEmpty()) {
                confirm.setEnabled(true);
                confirm.setAlpha(1.0f);
            } else {
                confirm.setEnabled(false);
                confirm.setAlpha(0.5f);
            }
        });
        phoneTf2.addTextObserver((text, i, i1, i2) -> {
            if (!text.isEmpty() && !phoneTf1.getText().isEmpty()) {
                confirm.setEnabled(true);
                confirm.setAlpha(1.0f);
            } else {
                confirm.setEnabled(true);
                confirm.setAlpha(0.5f);
            }
        });
        confirm.setClickedListener(component -> {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("urgent1", nameTf1.getText());
            zsonObject.put("urgentPhone1", phoneTf1.getText());
            zsonObject.put("urgent2", nameTf2.getText());
            zsonObject.put("urgentPhone2", phoneTf2.getText());
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            boolean isSuccess = false;
            try {
                isSuccess = mUrgentAbility.updateForm(mFormId, formBindingData);
                HiLog.info(TAG, "confirm: formId=" + mFormId + ",zsonObject=" + zsonObject.toString());
            } catch (FormException e) {
                e.printStackTrace();
            }
            if (isSuccess) {
                terminateAbility();
            } else {
                Toast.makeToast(this, "失败"+mFormId, Toast.TOAST_SHORT).show();
            }

        });
        cancel.setClickedListener(component -> {
            terminate();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void setFormId(long formId) {
        mFormId=formId;
    }
}
